from langchain_milvus import Milvus
from langchain_ollama import OllamaEmbeddings

# 连接 Milvus
embedder = OllamaEmbeddings(model="bge-m3:latest", base_url="http://192.168.7.3:11434")

vectorstore = Milvus(
    embedding_function=embedder,
    collection_name="knowledge",
    connection_args={
        "uri": "http://192.168.6.20:19530",
        "db_name": "ipp_air_general",
    },
    auto_id=True,
)


def insert_into_milvus(child_docs):
    """存储子文档到 Milvus"""
    child_embeddings = [
        (doc["text"], embedder.embed_query(doc["text"]), doc["parent_id"])
        for doc in child_docs
    ]
    texts, embeddings, metadata = zip(*child_embeddings)

    vectorstore.add_texts(
        texts=texts, metadatas=[{"parent_id": pid} for pid in metadata]
    )
    print(f"✅ 存入 {len(child_docs)} 个子文档到 Milvus")
